package 牛客_面试必刷.Month07.day09;

public class 二分查找1 {
    //首先，第一次写确实没写出来，这里太长时间没有复习，所以忘得有点多
    //再次重温一下，这里对于右指针的开和闭的情况

    //右指针为开，也就是娶不到的情况
    public int search1 (int[] nums, int target) {
        // write code here
        int i = 0;
        int j = nums.length;
        while(i < j){
            int index = (j + i) / 2;
            if(nums[index] > target){
                j = index;
            }else if(nums[index] < target){
                i = index + 1;
            }else{
                return index;
            }
        }
        return -1;
    }

    public int search2 (int[] nums, int target) {
        // write code here
        int i = 0;
        int j = nums.length - 1;
        while(i <= j){
            int index = (j + i) / 2;
            if(nums[index] > target){
                j = index - 1;
            }else if(nums[index] < target){
                i = index + 1;
            }else{
                return index;
            }
        }
        return -1;
    }
}
